package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsMapper;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NewsContentMapper contentMapper;

    public PageInfo queryForlist(NewsQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());

        return new PageInfo(newsMapper.queryForList(qo));
    }

    public void save(News news) {
        if(news.getState()==news.STATE_RELEASE){
            news.setReleaseTime(new Date());
        }
        newsMapper.insert(news);
        //保存文章内容
        NewsContent content = news.getContent();
        //设置id

        content.setId(news.getId());
        contentMapper.insert(content);
    }

    public void update(News news) {
        if(news.getState()==news.STATE_RELEASE){
            news.setReleaseTime(new Date());
        }
        newsMapper.updateByPrimaryKey(news);
        //保存文章内容
        NewsContent content = news.getContent();

        contentMapper.insert(content);
    }

    public NewsContent getNewsContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    public void changeState(News news) {
        if(news.getState()==news.STATE_RELEASE){
            news.setReleaseTime(new Date());
        }
        newsMapper.changState(news);
    }
}
